package com.wenhui.schoolyardinfo.login.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseActivity;
import com.wenhui.schoolyardinfo.base.BaseMvpActivity;
import com.wenhui.schoolyardinfo.customview.CircleImageView;
import com.wenhui.schoolyardinfo.login.presenter.LoginPresenterImpl;
import com.wenhui.schoolyardinfo.login.presenter.base.ILoginPresenter;
import com.wenhui.schoolyardinfo.login.view.ILoginView;
import com.wenhui.schoolyardinfo.login.view.base.IBaseView;
import com.wenhui.schoolyardinfo.main.MainActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/21 14:22
 * introduce :
 */

public class LoginActivity extends BaseMvpActivity<IBaseView, LoginPresenterImpl> implements ILoginView {

    private static final String TAG = "LoginActivity";
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.imv_head_portrait)
    CircleImageView imvHeadPortrait;
    @BindView(R.id.edt_input_account)
    EditText edtInputAccount;
    @BindView(R.id.edt_input_password)
    EditText edtInputPassword;
    @BindView(R.id.tev_forget_password)
    TextView tevForgetPassword;
    @BindView(R.id.tev_register_account)
    TextView tevRegisterAccount;
    @BindView(R.id.tev_login)
    TextView tevLogin;
    @BindView(R.id.tev_third_party_login)
    TextView tevThirdPartyLogin;
    @BindView(R.id.imv_qq_login)
    CircleImageView imvQqLogin;
    @BindView(R.id.imv_we_chat_login)
    CircleImageView imvWeChatLogin;
    @BindView(R.id.load_view)
    QMUILoadingView loadView;

    private Map<String, String> bodyParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        initData();
        initViews();
    }

    @Override
    protected void initData() {

//        LogManager.i(TAG, "initData*****uuid=" + DeviceUuidManager.getInstance(this).getDeviceUuid());
//        try {
//            ReadAndWriteManager.writeExternal(this, "mineData.txt", "uuid=" + DeviceUuidManager.getInstance(this).getDeviceUuid());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        bodyParams = new HashMap<>();
        presenter = initPresenter();
    }

    @Override
    protected void initViews() {
        setTopBar(topbar);
        topbar.removeAllLeftViews();

        loadView.setSize(120);
    }

    @Override
    protected LoginPresenterImpl initPresenter() {
        return new LoginPresenterImpl(this, this);
    }

    @Override
    public void showLoading() {
        loadView.setVisibility(View.VISIBLE);
        loadView.start();
    }

    @Override
    public void hideLoading() {
        loadView.stop();
        loadView.setVisibility(View.GONE);
    }

    @Override
    public void loginSuccess(String success) {

    }

    @Override
    public void loginError(String error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideLoading();
    }

    @OnClick({R.id.tev_forget_password, R.id.tev_register_account, R.id.tev_login, R.id.imv_qq_login, R.id.imv_we_chat_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tev_forget_password:
                startActivity(ForgetPasswordActivity.class);
                break;
            case R.id.tev_register_account:
                startActivity(RegisterNowActivity.class);
                break;
            case R.id.tev_login:

                String userName = edtInputAccount.getText().toString();
                String password = edtInputPassword.getText().toString();
                if (userName != null && !"".equals(userName)) {
                    if (password != null && !"".equals(password)) {
                        bodyParams.put("userName", userName);
                        bodyParams.put("password", password);
                        presenter.login(bodyParams);
                    } else {
                        showToast(getResources().getString(R.string.please_input_a_password));
                    }
                } else {
                    showToast(getResources().getString(R.string.please_enter_one_user_name));
                }

                startActivity(MainActivity.class);
                finish();
                break;
            case R.id.imv_qq_login:
                break;
            case R.id.imv_we_chat_login:
                break;
        }
    }
}
